package com.example.hw_smart_home;

import android.util.Log;

import com.example.hw_smart_home.huawei_api.auth.Authentication;
import com.example.hw_smart_home.huawei_api.util.Constants;
import com.example.hw_smart_home.huawei_api.util.HttpUtils;

import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ProductCommandIssued {
    @Test
    public void issueCommand() throws IOException {
        String projectId = Constants.PROJECT_ID_LT; //项目ID
        String deviceId = "67fa296d2902516e8670afd0_esp8266";  //设备ID
        String nodeId = "esp8266";  //设备标识码

        String url = String.format(Constants.Issue_COMMAND_URL, projectId, deviceId);

        String responseBody = getResponse(nodeId, url);

        if (responseBody == null || responseBody.isEmpty()) {
            System.out.println(responseBody);
        } else {
            System.out.println(responseBody);
        }
    }

    private String getResponse(String nodeId, String url) throws IOException {
        String token = Authentication.getToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Auth-Token", token);

/*        Map<String, String> params = new HashMap<>();
        params.put("node_id", nodeId);*/
        String contents = """
                {
                  "service_id" : "esp8266",
                  "command_name" : "Waring",
                  "paras" : {
                    "value" : "30.6"
                  }
                }""";

        HttpUtils httpUtils = new HttpUtils();
        return httpUtils.doPost(url, headers, contents);
    }
}
